//
//  OADLayoutControlPanel.m
//  OASDK_Works
//
//  Created by ChenJie on 3/15/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADLayoutControlPanel.h"
#import "OASDK.h"

@interface OADLayoutControlPanel ()

@property (nonatomic, strong) UISegmentedControl *directionControl;
@property (nonatomic, strong) UISegmentedControl *justifyContentControl;
@property (nonatomic, strong) UISegmentedControl *alignItemsControl;
@property (nonatomic, strong) NSArray *directionValues;
@property (nonatomic, strong) NSArray *justifyContentValues;
@property (nonatomic, strong) NSArray *alignItemsvalues;

@end

@implementation OADLayoutControlPanel

#pragma mark - Lifecycle methods

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.directionControl];
        [self addSubview:self.justifyContentControl];
        [self addSubview:self.alignItemsControl];
    }
    return self;
}

- (instancetype)initWithDirection:(Direction)direction
                   justifyContent:(JustifyContent)justifyContent
                       alignItems:(AlignItems)alignItems {
    
    self = [self initWithFrame:CGRectZero];
    if (self) {
        self.layoutDirection = direction;
        self.layoutJustifyContent = justifyContent;
        self.layoutAlignItems = alignItems;
    }
    return self;
}

+ (instancetype)controlPanelWithDirection:(Direction)direction
                           justifyContent:(JustifyContent)justifyContent
                               alignItems:(AlignItems)alignItems {
    
    return [[OADLayoutControlPanel alloc] initWithDirection:direction
                                             justifyContent:justifyContent
                                                 alignItems:alignItems];
    
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
}

- (void)didMoveToSuperview {
    [super didMoveToSuperview];
}

- (void)willMoveToWindow:(UIWindow *)newWindow {
    [super willMoveToWindow:newWindow];
}

- (void)didMoveToWindow {
    [super didMoveToWindow];
}

#define kControlMarginTop 5
#define kControlHeight 30

- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    CGFloat marginH = 5;
    CGFloat topMark = 0;
    CGFloat viewContainerWidth = self.oa_width - marginH * 2;
    
    //
    topMark += kControlMarginTop;
    self.directionControl.frame = CGRectMake(marginH, topMark, viewContainerWidth, kControlHeight);
    topMark += kControlHeight;
    
    //
    topMark += kControlMarginTop;
    self.justifyContentControl.frame = CGRectMake(marginH, topMark, viewContainerWidth, kControlHeight);
    topMark += kControlHeight;
    
    //
    topMark += kControlMarginTop;
    self.alignItemsControl.frame = CGRectMake(marginH, topMark, viewContainerWidth, kControlHeight);
//    topMark += kControlHeight;
    
}

#pragma mark - Public methods

+ (CGFloat)controlPanelHeight {
    return kControlHeight * 3 + kControlMarginTop * 4;
}

#pragma mark - Private methods

- (NSArray *)titlesWithItems:(NSArray *)items {
    NSMutableArray *titles = [NSMutableArray array];
    [items enumerateObjectsUsingBlock:^(NSDictionary *item, NSUInteger idx, BOOL * _Nonnull stop) {
        [titles addObject:item[@"title"]];
    }];
    return titles;
}

- (NSInteger)selectedIndexWithItems:(NSArray *)items value:(NSInteger)value {
    __block NSInteger selectedIndex = 0;
    [items enumerateObjectsUsingBlock:^(NSDictionary *item, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger itemValue = [item[@"value"] integerValue];
        if (itemValue == value) {
            selectedIndex = idx;
            *stop = YES;
        }
    }];
    return selectedIndex;
}

#pragma mark - Event response methods

- (void)directionChanged:(UISegmentedControl *)control {
    NSDictionary *selectedItem = self.directionValues[control.selectedSegmentIndex];
    self.layoutDirection = [selectedItem[@"value"] integerValue];
    !self.layoutDidChange ?: self.layoutDidChange(self.layoutDirection, self.layoutJustifyContent, self.layoutAlignItems);
}

- (void)justifyContentChanged:(UISegmentedControl *)control {
    NSDictionary *selectedItem = self.justifyContentValues[control.selectedSegmentIndex];
    self.layoutJustifyContent = [selectedItem[@"value"] integerValue];
    !self.layoutDidChange ?: self.layoutDidChange(self.layoutDirection, self.layoutJustifyContent, self.layoutAlignItems);
}

- (void)alignItemsChanged:(UISegmentedControl *)control {
    NSDictionary *selectedItem = self.alignItemsvalues[control.selectedSegmentIndex];
    self.layoutAlignItems = [selectedItem[@"value"] integerValue];
    !self.layoutDidChange ?: self.layoutDidChange(self.layoutDirection, self.layoutJustifyContent, self.layoutAlignItems);
}

#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setLayoutDirection:(Direction)layoutDirection {
    _layoutDirection = layoutDirection;
    
    self.directionControl.selectedSegmentIndex = [self selectedIndexWithItems:self.directionValues
                                                                        value:layoutDirection];
}

- (void)setLayoutJustifyContent:(JustifyContent)layoutJustifyContent {
    _layoutJustifyContent = layoutJustifyContent;
    
    self.justifyContentControl.selectedSegmentIndex = [self selectedIndexWithItems:self.justifyContentValues
                                                                             value:layoutJustifyContent];
}

- (void)setLayoutAlignItems:(AlignItems)layoutAlignItems {
    _layoutAlignItems = layoutAlignItems;
    
    self.alignItemsControl.selectedSegmentIndex = [self selectedIndexWithItems:self.alignItemsvalues
                                                                         value:layoutAlignItems];
}

#pragma mark - Getter

- (UISegmentedControl *)directionControl {
    if (!_directionControl) {
        NSArray *directions = [self titlesWithItems:self.directionValues];
        UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:directions];
        control.selectedSegmentIndex = [self selectedIndexWithItems:self.directionValues
                                                              value:self.layoutDirection];
        
        [control addTarget:self
                    action:@selector(directionChanged:)
          forControlEvents:UIControlEventValueChanged];
        
        _directionControl = control;
    }
    return _directionControl;
}

- (UISegmentedControl *)justifyContentControl {
    if (!_justifyContentControl) {
        NSArray *justifyControls = [self titlesWithItems:self.justifyContentValues];
        UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:justifyControls];
        control.selectedSegmentIndex = [self selectedIndexWithItems:self.justifyContentValues
                                                              value:self.layoutJustifyContent];
        
        [control addTarget:self
                    action:@selector(justifyContentChanged:)
          forControlEvents:UIControlEventValueChanged];
        
        _justifyContentControl = control;
    }
    return _justifyContentControl;
}

- (UISegmentedControl *)alignItemsControl {
    if (!_alignItemsControl) {
        NSArray *alignItems = [self titlesWithItems:self.alignItemsvalues];
        UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:alignItems];
        control.selectedSegmentIndex = [self selectedIndexWithItems:self.alignItemsvalues
                                                              value:self.layoutAlignItems];
        
        [control addTarget:self
                    action:@selector(alignItemsChanged:)
          forControlEvents:UIControlEventValueChanged];
        
        _alignItemsControl = control;
    }
    return _alignItemsControl;
}

- (NSArray *)directionValues {
    return @[
        @{@"title": @"Row", @"value": @(DirectionRow)},
        @{@"title": @"Column", @"value": @(DirectionColumn)},
    ];
}

- (NSArray *)justifyContentValues {
    return @[
        @{@"title": @"FStart", @"value": @(JustifyContentFlexStart)},
        @{@"title": @"Center", @"value": @(JustifyContentCenter)},
        @{@"title": @"FEnd", @"value": @(JustifyContentFlexEnd)},
        @{@"title": @"SAround", @"value": @(JustifyContentSpaceAround)},
        @{@"title": @"SBetween", @"value": @(JustifyContentSpaceBetween)},
    ];
}

- (NSArray *)alignItemsvalues {
    return @[
        @{@"title": @"FStart", @"value": @(AlignItemsFlexStart)},
        @{@"title": @"Center", @"value": @(AlignItemsCenter)},
        @{@"title": @"FEnd", @"value": @(AlignItemsFlexEnd)},
    ];
}

#pragma mark - Memory management methods

- (void)dealloc {
}

@end
